package com.dao.impl;


import cn.hutool.core.date.DateUtil;
import com.dao.CommentService;
import com.entity.Comment;
import com.entity.vo.CommentVo;
import com.util.JdbcUtil;
import com.util.PageUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2022-03-23
 */
public class CommentServiceImpl implements CommentService {


	@Override
	public int insert(Comment comment) throws SQLException {
		return 0;
	}

	@Override
	public List<Comment> queryData(Comment comment) throws IllegalAccessException, SQLException, InstantiationException {
		String sql = "select * from comment where id = ? ";

		return  JdbcUtil.queryData(sql, Comment.class, new Object[]{comment.getId()});
	}

	@Override
	public int update(Comment comment) throws SQLException {
		String sql = "update comment set ";

		List<Object> help = new ArrayList<>();

		if(comment == null || comment.getId() == 0 || comment.getId() == null) {
			return 0;
		}

		if(comment.getToCommentId() != null) {  // 回复的评论
			sql += " to_comment_id = ? ,";
			help.add(comment.getToCommentId());
		}

		if(comment.getIsDeleted() != null) { // 是否删除
			sql += " is_deleted = ? ,";
			help.add(comment.getIsDeleted());
		}

		if(comment.getMemberId() != null) { // 评论用户
			sql += " member_id = ? ,";
			help.add(comment.getMemberId());
		}

		if(comment.getContent() != null) { // 评论内容
			sql += " content = ? ,";
			help.add(comment.getContent());
		}

		if(comment.getFloor() != null) { // 楼层
			sql += " floor = ? ,";
			help.add(comment.getFloor());
		}

		if(comment.getSongId() != null) { // 歌曲id
			sql += " song_id = ? ,";
			help.add(comment.getSongId());
		}

		if(comment.getSongListId() != null) { // 歌单id
			sql += " song_list_id = ? ,";
			help.add(comment.getSongListId());
		}

		if(comment.getTags() != null) { // 点赞数
			sql += " tags = ? ,";
			help.add(comment.getTags());
		}

		// 添加修改时间
		sql += " update_time = ? ";
		help.add(new Date());

		sql += " where id = ? ";
		help.add(comment.getId());
		return JdbcUtil.insertOrUpdateOrDelete(sql, help.toArray());
	}

	@Override
	public int deleteById(Object id) throws SQLException {
		return 0;
	}

	@Override
	public PageUtil<Comment> queryDataBypage(int currentPgae, int pageSize, Comment comment) throws IllegalAccessException, SQLException, InstantiationException {

		String sql = "select * from comment  where 1=1 ";
		ArrayList values = new ArrayList();
		if (comment!=null){
			if(comment.getId() != null && !"".equals(comment.getId())) {
				sql += " and id like ? ";
				values.add("%" + comment.getId() + "%");
			}

			if(comment.getMemberId() != null && !"".equals(comment.getMemberId())) {
				sql += " and member_id = ? ";
				values.add(comment.getMemberId());
			}

			if(comment.getToCommentId() != null && !"".equals(comment.getToCommentId())) {
				sql += " and to_comment_id = ? ";
				values.add(comment.getToCommentId());
			}

			if(comment.getIsDeleted() != null && !"".equals(comment.getIsDeleted())) {
				sql += " and is_deleted = ? ";
				values.add(comment.getIsDeleted());
			} else {
				sql += " and is_deleted != 1 ";  // 管理员删除的
			}

			// 时间处理  date_format(orderma.create_time, '%y-%m-%d')=#{curMounth}
			if((comment.getCreateTime() != null && !"".equals(comment.getCreateTime())) && (comment.getUpdateTime() != null && !"".equals(comment.getUpdateTime()))) {
				sql += " and  date_format(create_time, '%Y-%m-%d') between ? and ? ";
				values.add(comment.getCreateTime());
				values.add(comment.getUpdateTime());
			} else if(comment.getCreateTime() != null && !"".equals(comment.getCreateTime())){
				sql += " and  date_format(create_time, '%Y-%m-%d') > ? ";
				values.add(DateUtil.format(comment.getCreateTime(),"yyyy-MM-dd"));
			} else if(comment.getUpdateTime() != null && !"".equals(comment.getUpdateTime())){
				values.add(DateUtil.format(comment.getUpdateTime(), "yyyy-MM-dd"));
				sql += " and  date_format(create_time, '%Y-%m-%d') < ? ";
			}

		}

		return JdbcUtil.queryDataByPage(sql, Comment.class,  currentPgae,pageSize ,values.toArray());
	}

	/**
	 * 批量删除根据id
	 * @param ids
	 * @return 返回受影响的行数
	 * @throws SQLException
	 */
	@Override
	public int deletedIds(String[] ids) throws SQLException {
		String sql = "update comment set is_deleted = 1 where id in (0";

		List<Object> list = new ArrayList<>();
		for (int i = 0; i < ids.length; i++) {
			sql += ",?";
			list.add(ids[i]);
		}

		sql+=")";

		int i = JdbcUtil.insertOrUpdateOrDelete(sql, list.toArray());

		return i;
	}

	@Override
	public PageUtil<CommentVo> queryDataBypage2(int currentPgae, int pageSize, Comment comment) throws SQLException, InstantiationException, IllegalAccessException {
		String sql = "select com.*,mem.avater as memberAvatar,mem.nickname memberNickname,\n" +
				"mem2.avater toMemberAvatar, mem2.nickname toMemberNickname \n" +
				"from `comment` com inner join member mem on com.member_id = mem.member_id \n" +
				"\t\tleft join member mem2 on com.to_comment_id = mem2.member_id  where 1=1 ";

		ArrayList values = new ArrayList();
		if (comment!=null){
			if(comment.getId() != null && !"".equals(comment.getId())) {
				sql += " and com.id like ? ";
				values.add("%" + comment.getId() + "%");
			}

			if(comment.getMemberId() != null && !"".equals(comment.getMemberId())) {
				sql += " and com.member_id = ? ";
				values.add(comment.getMemberId());
			}

			if(comment.getToCommentId() != null && !"".equals(comment.getToCommentId())) {
				sql += " and com.to_comment_id = ? ";
				values.add(comment.getToCommentId());
			}

			if(comment.getIsDeleted() != null && !"".equals(comment.getIsDeleted())) {
				sql += " and com.is_deleted = ? ";
				values.add(comment.getIsDeleted());
			} else {
				sql += " and com.is_deleted != 1 ";  // 管理员删除的
			}

			// 时间处理  date_format(orderma.create_time, '%y-%m-%d')=#{curMounth}
			if((comment.getCreateTime() != null && !"".equals(comment.getCreateTime())) && (comment.getUpdateTime() != null && !"".equals(comment.getUpdateTime()))) {
				sql += " and  date_format(com.create_time, '%Y-%m-%d') between ? and ? ";
				values.add(comment.getCreateTime());
				values.add(comment.getUpdateTime());
			} else if(comment.getCreateTime() != null && !"".equals(comment.getCreateTime())){
				sql += " and  date_format(com.create_time, '%Y-%m-%d') > ? ";
				values.add(DateUtil.format(comment.getCreateTime(),"yyyy-MM-dd"));
			} else if(comment.getUpdateTime() != null && !"".equals(comment.getUpdateTime())){
				values.add(DateUtil.format(comment.getUpdateTime(), "yyyy-MM-dd"));
				sql += " and  date_format(com.create_time, '%Y-%m-%d') < ? ";
			}

		}

		sql += " order by com.create_time ";

		return JdbcUtil.queryDataByPage(sql, CommentVo.class,  currentPgae,pageSize ,values.toArray());
	}
}
